package cn.hncj.lyc.controller.front;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.hncj.lyc.bean.UserInfo;
import cn.hncj.lyc.common.Result;
import cn.hncj.lyc.common.StatusCode;
import cn.hncj.lyc.service.UserService;


@RestController
@RequestMapping("/user")
public class UserLoginController {
	    @Autowired
	    private UserService userService;
	     /**
		 * 用户登录
		 * @param loginMap
		 * @return
		 */
        @RequestMapping(value="/login",method=RequestMethod.POST)
		public Result login(@RequestBody UserInfo userInfo,HttpSession session ,HttpServletRequest request){
			  //页面 name 表单对象  
			  String mobile = userInfo.getUserPhone();
		      System.out.println("打印手机号：--->"+mobile);
			  //手机号正则，判断输入是否为手机号
		      String ph = "^[1][3578]\\d{9}$";
			  System.out.println("正则---->"+mobile.matches(ph));
			  if (mobile.matches(ph)){
				UserInfo user = userService.findByMobileAndPassword(userInfo.getUserPhone(),userInfo.getUserPw());
				if(user!=null){					
					UserInfo loginuser = userService.doLogin(userInfo);
					//if(loginuser != null){
					session.setAttribute("loginuser", loginuser);
					return new Result(true,StatusCode.OK,"登陆成功",loginuser);
				}else{
					return new Result(false,StatusCode.LOGINERROR,"用户名或密码错误");
				}
			}
			else{
				return new Result(false,StatusCode.LOGINERROR,"用户名或密码错误");
			}
		}
		//request.getSession().setAttribute("user",user);
		  //String token = jwtUtil.createJWT(user.getId(), user.getNickname(), "user");
		//Map map=new HashMap();
		  //map.put("token",token);
		//map.put("name",user.getUserName());//姓名
		
//			}else {
//				UserInfo user =  userService.findByNameAndPassword(userInfo.getUserName(),userInfo.getUserPw());
//				if(user!=null){
//					//request.getSession().setAttribute("user",user);
//					  //String token = jwtUtil.createJWT(user.getId(), user.getNickname(), "user");
//					//Map map=new HashMap();
//					  //	map.put("token",token);
//					//map.put("name",user.getUserName());//姓名
//					UserInfo loginuser = userService.doLogin1(userInfo);
//					session.setAttribute("loginuser", loginuser);
//					return new Result(true,StatusCode.OK,"登陆成功",loginuser);
//
//				}else{
//					return new Result(false,StatusCode.LOGINERROR,"用户名或密码错误");
//				}
//			}
//		}

}
